<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="编码前缀" prop="codePrefix">
        <el-input
          v-model="queryParams.codePrefix"
          placeholder="请输入编码前缀"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生成类型" prop="codeGenerateType">
        <el-select
          v-model="queryParams.codeGenerateType"
          placeholder="请选择生成类型"
          clearable
        >
          <el-option
            v-for="dict in getSysCodeGenerateType"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="关联表单" prop="codeTableName">
        <el-select
          v-model="queryParams.codeTableName"
          placeholder="请选择关联表单"
          clearable
        >
          <el-option
            v-for="dict in getSysCodeTableName"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:code:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:code:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:code:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="codeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="关联表名"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="getSysCodeTableName"
            :value="scope.row.codeTableName"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="编码名称"
        align="center"
        prop="codeShowName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="编号前缀"
        align="center"
        prop="codePrefix"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="时间规则"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="getSysCodeTimeRule"
            :value="scope.row.codeTimeRule"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="生成类型"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="getSysCodeGenerateType"
            :value="scope.row.codeGenerateType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="初始流水号"
        align="center"
        prop="codeInitNum"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="流水号长度"
        align="center"
        prop="codeLength"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="步长"
        align="center"
        prop="codeStep"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="编号预览"
        align="center"
        :show-overflow-tooltip="true"
        width="160"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.codePreview }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:code:edit']"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="90px"
        label-position="left"
      >
        <el-form-item label="关联表单" prop="codeTableName">
          <el-select
            v-model="form.codeTableName"
            filterable
            placeholder="请选择关联表单"
            v-if="!form.codeId"
          >
            <el-option
              v-for="dict in getSysCodeTableName"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
          <el-input :value="form.codeTableName" :disabled="true" v-else />
        </el-form-item>
        <el-form-item label="编码名称" prop="codeShowName">
          <el-input
            v-model="form.codeShowName"
            placeholder="请输入编码名称"
            :disabled="form.codeId"
          />
        </el-form-item>
        <el-form-item label="编码前缀" prop="codePrefix">
          <el-input v-model="form.codePrefix" placeholder="请输入编码前缀" />
        </el-form-item>
        <el-form-item label="时间规则" prop="codeTimeRule">
          <el-select v-model="form.codeTimeRule" placeholder="请选择时间规则">
            <el-option
              v-for="dict in getSysCodeTimeRule"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="生成类型" prop="codeGenerateType">
          <el-radio-group v-model="form.codeGenerateType">
            <el-radio
              v-for="dict in dict.type.sys_code_generate_type"
              :key="dict.value"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="初始流水号" prop="codeInitNum">
          <el-input-number
            v-model="form.codeInitNum"
            :min="1"
            size="small"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="流水号长度" prop="codeLength">
          <el-input-number
            v-model="form.codeLength"
            :min="2"
            :max="7"
            size="small"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="步长" prop="codeStep">
          <el-input-number
            v-model="form.codeStep"
            :min="1"
            size="mini"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCode, getCode, addCode, updateCode } from '@/api/yyj/sys/code'

export default {
  name: 'Code',
  dicts: [
    'sys_code_generate_type',
    'sys_code_time_rule',
    'sys_code_table_name',
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        codePrefix: undefined,
        codeGenerateType: undefined,
        codeTableName: undefined,
      },
      // 编码表格数据
      codeList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        codeTableName: [
          { required: true, message: '关联表单不能为空', trigger: 'blur' },
        ],
        codeShowName: [
          { required: true, message: '编码名称不能为空', trigger: 'blur' },
        ],
        codePrefix: [
          { required: true, message: '编码前缀不能为空', trigger: 'blur' },
        ],
      },
    }
  },
  computed: {
    // 生成类型
    getSysCodeGenerateType() {
      return this.dict.type.sys_code_generate_type
    },
    // 时间规则
    getSysCodeTimeRule() {
      return this.dict.type.sys_code_time_rule
    },
    // 关联表单
    getSysCodeTableName() {
      return this.dict.type.sys_code_table_name
    },
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询编码列表 */
    getList() {
      this.loading = true
      listCode(this.queryParams).then(res => {
        this.codeList = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.codeId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加编码规则'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log('row', row)
      this.reset()
      const codeId = row.codeId || this.ids
      getCode(codeId).then(res => {
        let data = res.data
        this.form = {
          codeId: data.codeId,
          codeTableName: data.codeTableName,
          codeShowName: data.codeShowName,
          codePrefix: data.codePrefix,
          codeTimeRule: data.codeTimeRule,
          codeGenerateType: data.codeGenerateType,
          codeInitNum: data.codeInitNum,
          codeLength: data.codeLength,
          codeStep: data.codeStep,
        }
        this.open = true
        this.title = '修改编码规则'
      })
    },
    /** 表单重置 */
    reset() {
      this.form = {
        codeId: undefined,
        codeTableName: undefined,
        codeShowName: undefined,
        codePrefix: undefined,
        codeTimeRule: '0',
        codeGenerateType: '0',
        codeInitNum: 1,
        codeLength: 5,
        codeStep: 5,
      }
      this.resetForm('form')
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.codeId != undefined) {
            updateCode(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addCode(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        '/yyj/sys/code/export',
        {
          ...this.queryParams,
        },
        `code_${new Date().getTime()}.xlsx`
      )
    },
  },
}
</script>

<style></style>
